/**
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.text;

import static cc.aileron.workflow.WorkflowMethod.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.Collections;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import cc.aileron.junit.runner.guice.GuiceInjectRunner;
import cc.aileron.junit.runner.guice.GuiceInjectRunnerModule;
import cc.aileron.workflow.WorkflowBinder;
import cc.aileron.workflow.WorkflowConfigure;
import cc.aileron.workflow.container.WorkflowContainer;
import cc.aileron.workflow.container.WorkflowDto;
import cc.aileron.workflow.container.WorkflowFindCondition;
import cc.aileron.workflow.container.WorkflowRequestParameter;
import cc.aileron.workflow.environment.WorkflowEnvironment;
import cc.aileron.wsgi.test.WsgiTestModule;

import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * @author Aileron
 */
@GuiceInjectRunnerModule(value = WsgiTestModule.class, arg = "env.properties")
@RunWith(GuiceInjectRunner.class)
public class TryTextApplication
{
    /**
     * @throws ClassNotFoundException
     * @throws Exception
     */
    @Before
    public void before() throws ClassNotFoundException,
            Exception
    {
        final WorkflowConfigure configure = injector.getInstance(environment.getContainerConfigure());
        final WorkflowBinder binder = injector.getInstance(WorkflowBinder.class);
        configure.configure(binder);
    }

    /**
     * entry
     */
    @Test
    public void entry()
    {
        final WorkflowFindCondition condition = new WorkflowFindCondition();
        condition.method = POST;
        condition.uri = "/hogehoge/content.html";
        condition.parameter = new WorkflowRequestParameter()
        {
            @Override
            public Object get(final String name)
            {
                return null;
            }

            @SuppressWarnings("unchecked")
            @Override
            public Set<String> getKeys()
            {
                return Collections.EMPTY_SET;
            }
        };

        final WorkflowDto dto = container.get(condition);
        assertThat(dto.isNull(), is(false));

        dto.execute();

    }

    /**
     * index
     */
    @Test
    public void index()
    {
        final WorkflowFindCondition condition = new WorkflowFindCondition();
        condition.method = GET;
        condition.uri = "/";
        condition.parameter = new WorkflowRequestParameter()
        {
            @Override
            public Object get(final String name)
            {
                return null;
            }

            @SuppressWarnings("unchecked")
            @Override
            public Set<String> getKeys()
            {
                return Collections.EMPTY_SET;
            }
        };

        final WorkflowDto dto = container.get(condition);
        assertThat(dto.isNull(), is(false));
    }

    @Inject
    private WorkflowContainer container;

    @Inject
    private WorkflowEnvironment environment;

    @Inject
    private Injector injector;
}